<?php

namespace app\dm\traits;


use app\dm\services\InteriorCodeService;
use think\response\Json;

trait ResponseTrait
{
    /**
     * 正确响应
     * @param array $data
     * @param string $msg
     * @param int $statusCode
     * @param array $header
     * @param array $options
     * @return Json
     */
    protected function responseSuccess($data = [],$msg = '请求成功', $statusCode = InteriorCodeService::HTTP_OK, $header = [], array $options = [])
    {
        $ret = [
            'code' => InteriorCodeService::REQUEST_OK,
            'msg' => $msg,
        ];
        if (is_array($data) && !empty($data)) {
            $ret['result'] = $data;
        }
        return json($ret)->code($statusCode)->header($header)->options($options);
    }

    /**
     * 错误返回
     * @param string $msg
     * @param int $code
     * @param int $statusCode
     * @return Json
     */
    protected function responseFail($msg = '操作失败', $code = InteriorCodeService::REQUEST_FAILED, $statusCode = InteriorCodeService::HTTP_NOT_ACCEPTABLE)
    {
        $ret = [
            'code' => $code,
            'msg' => $msg,
        ];
        // return json($ret)->code($statusCode);
        // 错误http状态返回200，具体错误从返回信息的code判断
        return json($ret)->code(200);
    }

    /**
     * 验证错误响应
     * @param array $data
     * @param string $msg
     * @param int $statusCode
     * @return Json
     */
    protected function validateError($data = [], $msg = '参数验证失败', $statusCode = InteriorCodeService::HTTP_BAD_REQUEST)
    {
        $ret = [
            'code' => InteriorCodeService::VALIDATE_FAILED,
            'msg' => $msg,
        ];
        if (is_array($data) && !empty($data)) {
            $ret['data'] = $data;
        }
        // return json($ret)->code($statusCode);
        // 错误http状态返回200，具体错误从返回信息的code判断
        return json($ret)->code(200);
    }
}